package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.sggg.entity.SgTemplateRecipe;
import org.jeecg.modules.sggg.service.ISgTemplateRecipeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 教育处方模板
 * @Author: jeecg-boot
 * @Date: 2022-12-09
 * @Version: V1.0
 */
@Api(tags = "教育处方模板")
@RestController
@RequestMapping("/sggg/sgTemplateRecipe")
@Slf4j
public class SgTemplateRecipeController extends JeecgController<SgTemplateRecipe, ISgTemplateRecipeService> {
    @Autowired
    private ISgTemplateRecipeService sgTemplateRecipeService;

    @ApiOperation(value = "教育处方模板-分页列表查询", notes = "教育处方模板-分页列表查询")
    @GetMapping(value = "/list")
    public Result<List<SgTemplateRecipe>> queryPageList(SgTemplateRecipe sgTemplateRecipe,
                                                        HttpServletRequest req) {
        QueryWrapper<SgTemplateRecipe> queryWrapper = QueryGenerator.initQueryWrapper(sgTemplateRecipe, req.getParameterMap());
        List<SgTemplateRecipe> pageList = sgTemplateRecipeService.list(queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog(value = "教育处方模板-添加")
    @ApiOperation(value = "教育处方模板-添加", notes = "教育处方模板-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgTemplateRecipe sgTemplateRecipe) {
        sgTemplateRecipeService.save(sgTemplateRecipe);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "教育处方模板-编辑")
    @ApiOperation(value = "教育处方模板-编辑", notes = "教育处方模板-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgTemplateRecipe sgTemplateRecipe) {
        sgTemplateRecipeService.updateById(sgTemplateRecipe);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "教育处方模板-通过id删除")
    @ApiOperation(value = "教育处方模板-通过id删除", notes = "教育处方模板-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgTemplateRecipeService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "教育处方模板-批量删除")
    @ApiOperation(value = "教育处方模板-批量删除", notes = "教育处方模板-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgTemplateRecipeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "教育处方模板-通过id查询", notes = "教育处方模板-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgTemplateRecipe> queryById(@RequestParam(name = "id") String id) {
        SgTemplateRecipe sgTemplateRecipe = sgTemplateRecipeService.getById(id);
        if (sgTemplateRecipe == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgTemplateRecipe);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgTemplateRecipe sgTemplateRecipe) {
        return super.exportXls(request, sgTemplateRecipe, SgTemplateRecipe.class, "教育处方模板");
    }

}
